package com.example.demo.dismanage.service.impl;

import com.example.demo.dismanage.mapper.DisMapper;
import com.example.demo.dismanage.mapper.DisPaiMapper;
import com.example.demo.dismanage.service.DisPaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DisPaiServiceImpl implements DisPaiService {
    @Autowired
    private DisPaiMapper disPaiMapper;
    @Autowired
    private DisMapper disMapper;


    @Override
    public Map<String, Object> queryAllPaiDis(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disPaiMapper.queryAllPaiDisMapper(param);
        for(int i=0;i<resultList.size();i++){
            String YueStartDateStr = resultList.get(i).get("YueStartDate")!=null?resultList.get(i).get("YueStartDate").toString():null;
            String YueEndDateStr = resultList.get(i).get("YueEndDate")!=null?resultList.get(i).get("YueEndDate").toString():null;
            String PaiDateStr = resultList.get(i).get("PaiDate")!=null?resultList.get(i).get("PaiDate").toString():null;
            try {
                Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueStartDateStr);
                YueStartDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date2);

                Date date3 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(YueEndDateStr);
                YueEndDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date3);

                Date date4 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(PaiDateStr);
                PaiDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date4);
                resultList.get(i).put("YueStartDate",YueStartDateStr);
                resultList.get(i).put("YueEndDate",YueEndDateStr);
                resultList.get(i).put("PaiDate",PaiDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Map<String,String> countMap = disPaiMapper.queryAllPaiDisCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        return resultMap;
    }

    /**
     * @Description 派车返回
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> SaveFanInfo(Map<String, Object> param) {
        //更新车牌号与司机相同的单据为已返回状态
        List<Map<String,Object>> DocList = (List<Map<String, Object>>) param.get("selectYueS");
        Map<String,Object> DocMap = DocList.get(0);
        String SjNo = DocMap.get("SjNo")!=null?DocMap.get("SjNo").toString():null;
        String PaiCarNo = DocMap.get("PaiCarNo")!=null?DocMap.get("PaiCarNo").toString():null;
        param.put("PaiCarNo",PaiCarNo);
        param.put("PaiMan",SjNo);
        int i1 = disPaiMapper.SaveFanInfoMapper(param);

        //通过车牌号与司机员工编号查询单据
        List<Map<String,Object>> QDocList = disPaiMapper.queryAllDocBySjAndCarNo(param);

        //新增收费信息
        param.put("CostList",QDocList);
        int i2 = disPaiMapper.SaveCostInfoMapper(param);

        //修改车辆状态
        int i3 = disPaiMapper.updateCarStatusMapper(param);

        //修改司机状态
        int i4 = disPaiMapper.updateManStatusMapper(param);

        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i4==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;

    }

    /**
     * @Description 修改车辆返回接口
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> EditFan(Map<String, Object> param) {
        //更新车牌号与司机相同的单据为已返回状态
        int i1 = disPaiMapper.EditFanInfoMapper(param);

        //修改车辆信息
        int i2 = disPaiMapper.updateCarMapper(param);

        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i2==1){
            resultMap.put("Status","1");
        }else{
            resultMap.put("Status","0");
        }
        return resultMap;

    }

    /**
     * @Description 派车返回（手机端接口）
     * @Author zhangxue
     * @Date 17:34 2018/7/11
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> SaveFanInfoPhone(Map<String, Object> param) {
        //更新车牌号与司机相同的单据为已返回状态
        String SjNo = param.get("SjNo")!=null&&!"".equals(param.get("SjNo"))?param.get("SjNo").toString():null;
        String PaiCarNo = param.get("PaiCarNo")!=null&&!"".equals(param.get("PaiCarNo"))?param.get("PaiCarNo").toString():null;
        String FanEndKilo = param.get("FanKilometers")!=null&&!"".equals(param.get("FanKilometers"))?param.get("FanKilometers").toString():null;
        param.put("PaiCarNo",PaiCarNo);
        param.put("PaiMan",SjNo);
        //通过车牌号查询开始公里数
        Map<String,Object> KiloMap = new HashMap<String, Object>();
        KiloMap = disPaiMapper.queryStartKilo(param);
        String FanStartKilo = KiloMap.get("Mileage")!=null&&!"".equals(KiloMap.get("Mileage"))?KiloMap.get("Mileage").toString():null;
        param.put("FanStartKilo",FanStartKilo);
        param.put("FanEndKilo",FanEndKilo);
        Integer FanEndKl = Integer.valueOf(FanEndKilo);
        Integer FanStartKl = Integer.valueOf(FanStartKilo);
        if((FanEndKl-FanStartKl)<=0){
            Map<String,Object> tempMap = new HashMap<String, Object>();
            tempMap.put("Status","-2");//录入的公里数有错误
            return tempMap;
        }
        int FanKilometers = Integer.valueOf(FanEndKilo)-Integer.valueOf(FanStartKilo);
        param.put("FanKilometers",FanKilometers);
        //通过车牌号与司机员工编号查询单据
        List<Map<String,Object>> QDocList = disPaiMapper.queryAllDocBySjAndCarNo(param);
        int i1 = disPaiMapper.SaveFanInfoMapper(param);


        //新增收费信息
        param.put("CostList",QDocList);
        int i2 = disPaiMapper.SaveCostInfoMapper(param);

        //修改车辆状态
        int i3 = disPaiMapper.updateCarStatusMapper(param);

        //修改司机状态
        int i4 = disPaiMapper.updateManStatusMapper(param);

        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i4==1){
            List<Map<String,Object>> paiList = disMapper.queryAllPaiMan(param);
            resultMap.put("Status","1");
            resultMap.put("paiList",paiList);
        }else{
            resultMap.put("Status","0");
        }
        return resultMap;

    }

    /**
         * @Description 查询所有车辆返回信息
         * @Author zhangxue
         * @Date 9:58 2018/8/6
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryAllFanDis(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disPaiMapper.queryAllFanDisMapper(param);
        for(int i=0;i<resultList.size();i++){
            String FanDateStr = resultList.get(i).get("FanDate")!=null?resultList.get(i).get("FanDate").toString():null;
            resultList.get(i).put("FanDate",FanDateStr);
            String PaiDateStr = resultList.get(i).get("PaiDate")!=null?resultList.get(i).get("PaiDate").toString():null;
            resultList.get(i).put("PaiDate",PaiDateStr);
        }
        Map<String,String> countMap = disPaiMapper.queryAllFanDisCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("totalnum",totalSize);
        return resultMap;
    }

    /**
         * @Description 车辆返回信息数据统计
         * @Author zhangxue
         * @Date 9:35 2018/10/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryAllFanData(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disPaiMapper.queryAllFanDataMapper(param);
        Integer datatotal = 0;
        for(int i=0;i<resultList.size();i++){
            String FanDateStr = resultList.get(i).get("FanDate")!=null?resultList.get(i).get("FanDate").toString():null;
            resultList.get(i).put("FanDate",FanDateStr);
            String FanKilo = resultList.get(i).get("FanKilometers")!=null?resultList.get(i).get("FanKilometers").toString():"0";
            datatotal += Integer.valueOf(FanKilo);
        }
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        resultMap.put("datatotal",datatotal);
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> queryAllYueStore(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = disPaiMapper.queryAllYueStore(param);
        return resultList;
    }
}
